package com.jp.vn.main;

import java.util.ArrayList;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jp.vn.CustomAdapter.AoAmAdapter;
import com.jp.vn.CustomAdapter.LearnAlphabetAdapter;
import com.jp.vn.CustomAdapter.TrocAmAdapter;
import com.jp.vn.Object.Alphabet;

public class HiraganaActivity extends BaseActivity {

	ArrayList<Alphabet> arrayHiragana = new ArrayList<Alphabet>();
	ArrayList<Alphabet> arrayTrocAm = new ArrayList<Alphabet>();
	ArrayList<Alphabet> arrayAoAm = new ArrayList<Alphabet>();
	private TextView[] dots;
	private LinearLayout dotsLayout;
	ViewPager myViewPager;
	Button btnBangChuCai, btnTrocAm, btnAoAm;
	public String pager;
	ImageView btnMenuPopUp;
	LinearLayout menu_popup;
	int height = 0;
	int width = 0;
	int space = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alphabets);
		myViewPager = (ViewPager) findViewById(R.id.alphabets_view);
		 btnBangChuCai = (Button) findViewById(R.id.btnBangChuCai);
		 btnTrocAm = (Button) findViewById(R.id.btnTrocAm);
		 btnAoAm = (Button) findViewById(R.id.btnAoAm);
		dotsLayout = (LinearLayout) findViewById(R.id.dots_layout);
		btnMenuPopUp = (ImageView) findViewById(R.id.btnMenu);
		menu_popup = (LinearLayout) findViewById(R.id.menu_popup);

		dumyData();
		dumyDataTrocAm();
		dumyDataAoAm();

		setPageIndicator(arrayHiragana, 5);
		pager = "bangchucai";
		LearnAlphabetAdapter adapterHiragana = new LearnAlphabetAdapter(
				getApplicationContext(), arrayHiragana, 1);
		myViewPager.setAdapter(adapterHiragana);

		if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
			Toast.makeText(getApplicationContext(), "Small", Toast.LENGTH_SHORT)
					.show();
		}

		btnBangChuCai.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				pager = "bangchucai";
				btnBangChuCai
						.setBackgroundResource(R.drawable.tab_bcc_selected);
				btnAoAm.setBackgroundResource(R.drawable.tab_aoam_normal);
				btnTrocAm.setBackgroundResource(R.drawable.tab_trocam_normal);
				dotsLayout.removeAllViews();
				setPageIndicator(arrayHiragana, 5);
				LearnAlphabetAdapter adapterHiragana = new LearnAlphabetAdapter(
						getApplicationContext(), arrayHiragana, 1);
				myViewPager.setAdapter(adapterHiragana);
			}
		});

		btnTrocAm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				pager = "trocam";
				btnBangChuCai.setBackgroundResource(R.drawable.tab_bcc_normal);
				btnAoAm.setBackgroundResource(R.drawable.tab_aoam_normal);
				btnTrocAm.setBackgroundResource(R.drawable.tab_trocam_selected);
				dotsLayout.removeAllViews();
				setPageIndicator(arrayTrocAm, 5);
				TrocAmAdapter adapter = new TrocAmAdapter(
						getApplicationContext(), arrayTrocAm);
				myViewPager.setAdapter(adapter);

			}
		});

		btnAoAm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				pager = "aoam";
				btnBangChuCai.setBackgroundResource(R.drawable.tab_bcc_normal);
				btnAoAm.setBackgroundResource(R.drawable.tab_aoam_selected);
				btnTrocAm.setBackgroundResource(R.drawable.tab_trocam_normal);
				dotsLayout.removeAllViews();
				setPageIndicator(arrayAoAm, 3);
				AoAmAdapter adapter = new AoAmAdapter(getApplicationContext(),
						arrayAoAm);
				myViewPager.setAdapter(adapter);
			}
		});

		myViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				int num = 0;

				if (pager.equals("bangchucai")) {
					num = arrayHiragana.size() / 5;
				}

				if (pager.equals("trocam")) {
					num = arrayTrocAm.size() / 5;
				}

				if (pager.equals("aoam")) {
					num = arrayAoAm.size() / 3;
				}

				for (int i = 0; i < num; i++) {
					dots[i].setTextColor(getResources().getColor(
							android.R.color.darker_gray));
				}
				dots[position].setTextColor(getResources().getColor(
						android.R.color.white));
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		btnMenuPopUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				PopupMenu menu = new PopupMenu(view, height, width);
				menu.showPopup(space + 5);
			}
		});
	}

	void dumyData() {

		arrayHiragana.add(new Alphabet("あ", "a", 0));
		arrayHiragana.add(new Alphabet("い", "i", 0));
		arrayHiragana.add(new Alphabet("う", "u", 0));
		arrayHiragana.add(new Alphabet("え", "e", 0));
		arrayHiragana.add(new Alphabet("お", "o", 0));

		arrayHiragana.add(new Alphabet("か", "ka", 0));
		arrayHiragana.add(new Alphabet("き", "ki", 0));
		arrayHiragana.add(new Alphabet("く", "ku", 0));
		arrayHiragana.add(new Alphabet("け", "ke", 0));
		arrayHiragana.add(new Alphabet("こ", "ko", 0));

		arrayHiragana.add(new Alphabet("さ", "sa", 0));
		arrayHiragana.add(new Alphabet("し", "shi", 0));
		arrayHiragana.add(new Alphabet("す", "su", 0));
		arrayHiragana.add(new Alphabet("せ", "se", 0));
		arrayHiragana.add(new Alphabet("そ", "so", 0));

		arrayHiragana.add(new Alphabet("た", "ta", 0));
		arrayHiragana.add(new Alphabet("ち", "chi", 0));
		arrayHiragana.add(new Alphabet("つ", "tsu", 0));
		arrayHiragana.add(new Alphabet("て", "te", 0));
		arrayHiragana.add(new Alphabet("と", "to", 0));

		arrayHiragana.add(new Alphabet("な", "na", 0));
		arrayHiragana.add(new Alphabet("に", "ni", 0));
		arrayHiragana.add(new Alphabet("ぬ", "nu", 0));
		arrayHiragana.add(new Alphabet("ね", "ne", 0));
		arrayHiragana.add(new Alphabet("の", "no", 0));

		arrayHiragana.add(new Alphabet("は", "ha", 0));
		arrayHiragana.add(new Alphabet("ひ", "hi", 0));
		arrayHiragana.add(new Alphabet("ふ", "fu", 0));
		arrayHiragana.add(new Alphabet("へ", "he", 0));
		arrayHiragana.add(new Alphabet("ほ", "ho", 0));

		arrayHiragana.add(new Alphabet("ま", "ma", 0));
		arrayHiragana.add(new Alphabet("み", "mi", 0));
		arrayHiragana.add(new Alphabet("む", "mu", 0));
		arrayHiragana.add(new Alphabet("め", "me", 0));
		arrayHiragana.add(new Alphabet("も", "mo", 0));

		arrayHiragana.add(new Alphabet("や", "ya", 0));
		arrayHiragana.add(new Alphabet("", "", 0));
		arrayHiragana.add(new Alphabet("ゆ", "yu", 0));
		arrayHiragana.add(new Alphabet("", "", 0));
		arrayHiragana.add(new Alphabet("よ", "yo", 0));

		arrayHiragana.add(new Alphabet("ら", "ra", 0));
		arrayHiragana.add(new Alphabet("り", "ri", 0));
		arrayHiragana.add(new Alphabet("る", "ru", 0));
		arrayHiragana.add(new Alphabet("れ", "re", 0));
		arrayHiragana.add(new Alphabet("ろ", "ro", 0));

		arrayHiragana.add(new Alphabet("わ", "wa", 0));
		arrayHiragana.add(new Alphabet("", "", 0));
		arrayHiragana.add(new Alphabet("を", "wo", 0));
		arrayHiragana.add(new Alphabet("", "", 0));
		arrayHiragana.add(new Alphabet("ん", "n", 0));
	}

	void dumyDataTrocAm() {

		arrayTrocAm.add(new Alphabet("が", "ga", 0));
		arrayTrocAm.add(new Alphabet("ぎ", "gi", 0));
		arrayTrocAm.add(new Alphabet("ぐ", "gu", 0));
		arrayTrocAm.add(new Alphabet("げ", "ge", 0));
		arrayTrocAm.add(new Alphabet("ご", "go", 0));

		arrayTrocAm.add(new Alphabet("ざ", "za", 0));
		arrayTrocAm.add(new Alphabet("じ", "ji", 0));
		arrayTrocAm.add(new Alphabet("ず", "zu", 0));
		arrayTrocAm.add(new Alphabet("ぜ", "ze", 0));
		arrayTrocAm.add(new Alphabet("ぞ", "zo", 0));

		arrayTrocAm.add(new Alphabet("だ", "da", 0));
		arrayTrocAm.add(new Alphabet("ぢ", "ji", 0));
		arrayTrocAm.add(new Alphabet("づ", "zu", 0));
		arrayTrocAm.add(new Alphabet("で", "de", 0));
		arrayTrocAm.add(new Alphabet("ど", "do", 0));

		arrayTrocAm.add(new Alphabet("ば", "ba", 0));
		arrayTrocAm.add(new Alphabet("び", "bi", 0));
		arrayTrocAm.add(new Alphabet("ぶ", "bu", 0));
		arrayTrocAm.add(new Alphabet("べ", "be", 0));
		arrayTrocAm.add(new Alphabet("ぼ", "bo", 0));

		arrayTrocAm.add(new Alphabet("ぱ", "pa", 0));
		arrayTrocAm.add(new Alphabet("ぴ", "pi", 0));
		arrayTrocAm.add(new Alphabet("ぷ", "pu", 0));
		arrayTrocAm.add(new Alphabet("ぺ", "pe", 0));
		arrayTrocAm.add(new Alphabet("ぽ", "po", 0));
	}

	void dumyDataAoAm() {

		arrayAoAm.add(new Alphabet("きゃ", "kya", 0));
		arrayAoAm.add(new Alphabet("きゅ", "kyu", 0));
		arrayAoAm.add(new Alphabet("きょ", "kyo", 0));

		arrayAoAm.add(new Alphabet("ぎゃ", "gya", 0));
		arrayAoAm.add(new Alphabet("ぎゅ", "gyu", 0));
		arrayAoAm.add(new Alphabet("ぎょ", "gyo", 0));

		arrayAoAm.add(new Alphabet("しゃ", "sha", 0));
		arrayAoAm.add(new Alphabet("しゅ", "shu", 0));
		arrayAoAm.add(new Alphabet("しょ", "sho", 0));

		arrayAoAm.add(new Alphabet("じゃ", "ja", 0));
		arrayAoAm.add(new Alphabet("じゅ", "ju", 0));
		arrayAoAm.add(new Alphabet("じょ", "jo", 0));

		arrayAoAm.add(new Alphabet("ちゃ", "cha", 0));
		arrayAoAm.add(new Alphabet("ちゅ", "chu", 0));
		arrayAoAm.add(new Alphabet("ちょ", "cho", 0));

		arrayAoAm.add(new Alphabet("にゃ", "nya", 0));
		arrayAoAm.add(new Alphabet("にゅ", "nyu", 0));
		arrayAoAm.add(new Alphabet("にょ", "nyo", 0));

		arrayAoAm.add(new Alphabet("ひゃ", "hya", 0));
		arrayAoAm.add(new Alphabet("ひゅ", "hyu", 0));
		arrayAoAm.add(new Alphabet("ひょ", "hyo", 0));

		arrayAoAm.add(new Alphabet("びゃ", "bya", 0));
		arrayAoAm.add(new Alphabet("びゅ", "byu", 0));
		arrayAoAm.add(new Alphabet("びょ", "byo", 0));

		arrayAoAm.add(new Alphabet("ぴゃ", "pya", 0));
		arrayAoAm.add(new Alphabet("ぴゅ", "pyu", 0));
		arrayAoAm.add(new Alphabet("ぴょ", "pyo", 0));

		arrayAoAm.add(new Alphabet("みゃ", "mya", 0));
		arrayAoAm.add(new Alphabet("みゅ", "myu", 0));
		arrayAoAm.add(new Alphabet("みょ", "myo", 0));

		arrayAoAm.add(new Alphabet("りゃ", "rya", 0));
		arrayAoAm.add(new Alphabet("りゅ", "ryu", 0));
		arrayAoAm.add(new Alphabet("りょ", "ryo", 0));
	}

	public void setPageIndicator(ArrayList<Alphabet> arrayAlphabet, int numCol) {
		dots = new TextView[arrayAlphabet.size() / numCol];
		for (int i = 0; i < arrayAlphabet.size() / numCol; i++) {
			dots[i] = new TextView(this);
			dots[i].setText(Html.fromHtml("&#8226;"));
			Configuration config = getResources().getConfiguration();

			if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
				dots[i].setTextSize(25);
			}
			if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
				dots[i].setTextSize(30);
			}

			if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
				dots[i].setTextSize(40);
			}

			dots[i].setTextColor(getResources().getColor(
					android.R.color.darker_gray));
			dotsLayout.addView(dots[i]);
		}
		dots[0].setTextColor(getResources().getColor(android.R.color.white));
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		width = menu_popup.getWidth();
		menu_popup.setVisibility(View.GONE);
		height = (int) getResources().getDimension(R.dimen.menupopup_height);
		space = (int) getResources().getDimension(R.dimen.menu_height);
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
		finish();
	}
}
